import { CommonStatus } from "../../models";
import { DataGroupItem, DeptItem, DeptType, DictItem, DictTypeCode, MenuItem, RouteItem, UserItem } from "./models";
import req from "/@/api/req";
import { SelectOptionItem } from "/@/types/antd";

export const Api = {
  menuList: "zyx-system/api/v1/menus",
  routes: "zyx-system/api/v1/menus/routes",

  paramPage: "zyx-system/api/v1/param/page",
  param: "zyx-system/api/v1/param",

  dictPage: "zyx-system/api/v1/dict/page",
  dict: "zyx-system/api/v1/dict",
  dictOptions: "zyx-system/api/v1/dict/options",

  dept: "zyx-system/api/v1/dept",

  rolesPage: "zyx-system/api/v1/roles/page",
  roles: "zyx-system/api/v1/roles",
  rolesMenuIds: "zyx-system/api/v1/roles/{roleId}/menuIds",
  rolesOptions: "zyx-system/api/v1/roles/options",

  usersPage: "zyx-system/api/v1/users/page",
  users: "zyx-system/api/v1/users",
  usersForm: "zyx-system/api/v1/users/{userId}/form",
  usersPassword: "zyx-system/api/v1/users/{userId}/password",
  userStatus: "zyx-system/api/v1/users/{userId}/status",

  dataGroupPage: "zyx-system/api/v1/data-group/page",
  dataGroup: "zyx-system/api/v1/data-group",
  dataGroupList: "zyx-system/api/v1/data-group/list",
};

export function getRoutes() {
  return req.commonGet<RouteItem[]>(Api.routes, {});
}

export function getRoleMenuIds(roleId: number) {
  return req.commonGet<number[]>(Api.rolesMenuIds.replace("{roleId}", roleId + ""), {}, { useGlobalLoading: true });
}

export function getMenuList() {
  return req.commonGet<MenuItem[]>(Api.menuList, {}, { useGlobalLoading: true });
}

export function getDeptList(params: { type: DeptType }) {
  return req.commonGet<DeptItem[]>(Api.dept, params, { useGlobalLoading: false });
}

export function getDictOptions(params: { typeCode: DictTypeCode }) {
  return req.commonGet<SelectOptionItem[]>(Api.dictOptions, params, { useGlobalLoading: false });
}

export function getRoleOptions() {
  return req.commonGet<SelectOptionItem[]>(Api.rolesOptions, {}, { useGlobalLoading: false });
}

export function getUserDetail(userId: number) {
  return req.commonGet<UserItem>(Api.usersForm.replace("{userId}", userId + ""), {}, { useGlobalLoading: false });
}

export function getDataGroupList() {
  return req.commonGet<DataGroupItem[]>(Api.dataGroupList, {}, { useGlobalLoading: false });
}

export function changeUserStatus(userId: number, status: CommonStatus) {
  return req.patch(
    {
      url: Api.userStatus.replace("{userId}", userId + ""),
      params: {
        status,
      },
    },
    {
      useGlobalLoading: false,
    }
  );
}
